# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
import json
import csv
import sys
reload(sys)
sys.setdefaultencoding('utf-8')


class ManhuaPipeline(object):
    def open_spider(self, spider):
        self.file_csv = open('aaaa.csv', 'w')
        self.obj_csv = csv.writer(self.file_csv)
        self.first = True
        # self.obj_csv.writerrow(['书名', '链接'])

    def process_item(self, item, spider):
        if self.first:
            self.obj_csv.writerow(['书名', '链接'])
            self.first = False
        # self.obj_csv.writerow(item['page'])
        self.obj_csv.writerow([item['name'][0], item['url_part'][0]])
        return item

    def close_spider(self, spider):
        self.file_csv.close()
